package org.example.examfx02.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.example.examfx02.entity.Student;
import org.example.examfx02.tool.StringUtil;
import org.example.examfx02.tool.SystemData;

public class AddStuViewController {
    SystemData sys = SystemData.getInstance();
    @FXML
    TextField stuId;
    @FXML
    TextField stuClass;
    @FXML
    TextField stuName;
    @FXML
    TextField stuPassword;
    @FXML
    Button admitBtn2;
    @FXML
    Label label;
    public void initialize(){
        label.setVisible(false);
    }
    @FXML
    public void admit(){
        String id = stuId.getText();
        String myClass = stuClass.getText();
        String name = stuName.getText();
        String password = stuPassword.getText();
        if(StringUtil.isEmpty(id)){
            label.setText("ID不能为空");
            label.setVisible(true);
            return ;
        }else if(sys.getUserGroup().getStudentById(id)!=null){
            label.setText("ID已被注册");
            label.setVisible(true);
            return ;
        }
        if(StringUtil.isEmpty(myClass)){
            label.setText("班级不能为空");
            label.setVisible(true);
            return ;
        }
        if(StringUtil.isEmpty(name)){
            label.setText("姓名不能为空");
            label.setVisible(true);
            return ;
        }
        if(StringUtil.isEmpty(password)){
            label.setText("密码不能为空");
            label.setVisible(true);
            return ;
        }
        label.setVisible(false);
        Student student = new Student(name,id,myClass,password,true);
        SystemData.getInstance().getUserGroup().getStudents().add(student);

    }
}
